package drds.server.sqlparser.sqlobject.statement.insert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import drds.server.sqlparser.sqlobject.AbstractSqlObject;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.sqlobject.expression.name.IName;
import drds.server.sqlparser.sqlobject.statement.Statement;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class InsertStatement extends AbstractSqlObject implements Statement {

	protected IName tableName;
	/**
	 * 在进行插入数据的时候,首先判断是否含有切分键,如果没有切分键则不允许执行。现在DRDS只支持一维和二维切分。
	 */
	protected final List<Expression> columnNameList = new ArrayList<Expression>();
	protected HashSet<String> columnNameSet = new HashSet<String>();
	protected HashMap<String, Integer> columnNameIndexMap = new HashMap<String, Integer>();

	public HashMap<String, Integer> getColumnNameIndexMap() {
		return columnNameIndexMap;
	}

	/**
	 * 如果存在?则可以进行批量替换?进行执行。
	 */
	protected ValueList valueList;

	public void setTableName(IName tableName) {
		if (tableName != null) {
			tableName.setParent(this);
		}
		this.tableName = tableName;
	}

	public IName getTableName() {
		return tableName;
	}

	public void addColumn(Expression columnName) {
		if (columnName != null) {
			columnName.setParent(this);
		}
		this.columnNameList.add(columnName);
	}

	public List<Expression> getColumnNameList() {
		return columnNameList;
	}

	public void createColumnNameset() {
		int index = 0;
		for (Expression columnName : this.columnNameList) {
			this.columnNameSet.add(columnName.toString());
			this.columnNameIndexMap.put(columnName.toString(), index++);
		}
		if (this.columnNameList.size() != this.columnNameSet.size()) {
			throw new RuntimeException("");
		}
	}

	public HashSet<String> getColumnNameSet() {
		return columnNameSet;
	}

	public ValueList getValueList() {
		return valueList;
	}

	public void setValueList(ValueList valueList) {
		this.valueList = valueList;
	}

	@Override
	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			this.acceptChild(visitor, tableName);
			this.acceptChild(visitor, columnNameList);
			this.acceptChild(visitor, valueList);
		}

		visitor.endVisit(this);
	}

}
